import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';
import 'package:qaun_ni_dev/pages/active/active_detail.dart';
import 'package:qaun_ni_dev/provider/active_provider.dart';

class ActivePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ActivePageList();
  }
}


class SortCondition2 {
  String name;
  bool isSelected;
  int  typeId;
  SortCondition2({this.name, this.isSelected,this.typeId});
}

class ActivePageList extends StatefulWidget {
  @override
  _ActivePageListState createState() => _ActivePageListState();
}

class _ActivePageListState extends State<ActivePageList> {


  List<String> _dropDownHeaderItemStrings = ['动态类型'];

  List<SortCondition2> _typeSortConditions = []; //动态数据

  SortCondition2 _selectTypeSortCondition;

  var _scaffoldKey33 = new GlobalKey<ScaffoldState>();
  GlobalKey _stackKey33 = GlobalKey();
  GZXDropdownMenuController _dropdownMenuController2 = GZXDropdownMenuController();

  void initState() {
    super.initState();
    _typeSortConditions.add(SortCondition2(typeId: 0,name: '全部',isSelected: true));
    _typeSortConditions.add(SortCondition2(typeId: 0,name: '日常动态',isSelected: false));
    _typeSortConditions.add(SortCondition2(typeId: 1,name: '配音视频',isSelected: false));
    _typeSortConditions.add(SortCondition2(typeId: 2,name: '注册审核视频',isSelected: false));
    _typeSortConditions.add(SortCondition2(typeId: 3,name: '认证视频',isSelected: false));
    _selectTypeSortCondition = _typeSortConditions[0];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey33,
      appBar: AppBar(
        title: Text('用户动态列表'),
      ),
      body: Stack(
        key: _stackKey33,
        children: <Widget>[
          Column(
            children: <Widget>[
              GZXDropDownHeader(
                items: [
                  GZXDropDownHeaderItem(_dropDownHeaderItemStrings[0]),
                ],
                stackKey: _stackKey33,
                controller: _dropdownMenuController2,
                onItemTap: (index) {
                  print(index);
                },
              ),
              Container(
                child: Expanded(
                    child: ActiveList()
                ),
              )
            ],
          ),
          GZXDropDownMenu(
            controller: _dropdownMenuController2,
            animationMilliseconds: 400,
            menus: [
              GZXDropdownMenuBuilder(
                  dropDownHeight: 40 * 8.0,
                  dropDownWidget: _buildConditionListWidget(_typeSortConditions,(value){
                    _selectTypeSortCondition = value;
                    _dropDownHeaderItemStrings[0] = _selectTypeSortCondition.name == '全部'? '动态类型':_selectTypeSortCondition.name;
                    _dropdownMenuController2.hide();
                    setState(() {
                      if(_selectTypeSortCondition.name == '全部') {
                        Provider.of<ActiveProvider>(context,listen: false).changeTypeValue('');
                        Provider.of<ActiveProvider>(context,listen: false).getActiveList(context);
                      } else {
                        Provider.of<ActiveProvider>(context,listen: false).changeTypeValue(_selectTypeSortCondition.typeId);
                        Provider.of<ActiveProvider>(context,listen: false).getActiveList(context);
                      }
                      print('${_selectTypeSortCondition.typeId}');
                    });
                  })
              ),
            ],
          )
        ],
      ),
    );

  }
  _buildConditionListWidget(items, void itemOnTap(SortCondition2 sortCondition)) {
    return ListView.builder(
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      itemCount: items.length,
      itemBuilder: (BuildContext context,int index) {
        SortCondition2 typeSortCondition = items[index];
        return InkWell(
          onTap: () {
            for (var value in items) {
              value.isSelected = false;
            }
            typeSortCondition.isSelected = true;
            itemOnTap(typeSortCondition);
          },
          child: Container(
            height: 40,
            child: Row(
              children: <Widget>[
                SizedBox(
                  width: 16,
                ),
                Expanded(
                  child: Text(
                    typeSortCondition.name,
                    style: TextStyle(
                        color: typeSortCondition.isSelected ? Colors.black26: Colors.black
                    ),
                  ),
                ),
                typeSortCondition.isSelected ? Icon(Icons.check):SizedBox(),
                SizedBox(
                  width: 16,
                )
              ],
            ),
          ),
        );

      },
    );
  }




}




// 列表数据
class ActiveList extends StatefulWidget {
  @override
  _ActiveListState createState() => _ActiveListState();
}

class _ActiveListState extends State<ActiveList> {
  GlobalKey<RefreshFooterState> _footerKey55 = new GlobalKey<RefreshFooterState>();
  GlobalKey<RefreshHeaderState> _headerKey55 = new GlobalKey<RefreshHeaderState>();

  void initState() {
    super.initState();
    _getMoreList();
  }

  void _getMoreList() {
    Provider.of<ActiveProvider>(context, listen: false).getActiveList(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 620,
      padding: EdgeInsets.only(top: 10, left: 15, right: 15),
      child: Consumer<ActiveProvider>(
        builder: (context, data, _) {
          if (data.activeListData.length > 0) {
            return Container(
              child: EasyRefresh(
                  refreshFooter: ClassicsFooter(
                      key: _footerKey55,
                      bgColor: Colors.white,
                      textColor: Colors.pink,
                      moreInfoColor: Colors.pink,
                      showMore: true,
                      noMoreText: '没有更多了',
                      moreInfo: '加载中',
                      loadReadyText: '上拉加载'),
                  refreshHeader: ClassicsHeader(
                      key: _headerKey55,
                      refreshText: "下拉刷新数据",
                      refreshReadyText: "准备刷新数据",
                      refreshingText: "数据刷新中...",
                      refreshedText: "刷新完成",
                      bgColor: Colors.pink,
                      textColor: Colors.white,
                      moreInfoColor: Colors.white,
                      refreshHeight: 70.0,
                      isFloat: false,
                      showMore: false,
                      moreInfo: "Updated at %T"),
                  child: ListView.builder(
                      itemCount: data.activeListData.length,
                      itemBuilder: (context, index) {
                        return _listWidget(data.activeListData, index);
                      }),
                  loadMore: () async {
                    print('加载更多的');
                    Provider.of<ActiveProvider>(context, listen: false).getActiveMoreList(context);
                  },
                  onRefresh: () async {
                    print('上拉刷新');
                    Provider.of<ActiveProvider>(context, listen: false).getActiveList(context);
                  }),
            );
          } else {
            return Text('请稍等，数据加载中或暂无数据');
          }
        },
      ),
    );
  }
  Widget _listWidget (List activeList,int index) {
    return InkWell(
      onTap: () {
        Navigator.push(context, MaterialPageRoute(
            builder: (context) {
              return ActiveDetail(activeList[index]);
            }
        ));
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
            border: Border.all(width: 1,color: Colors.black12),
            borderRadius: BorderRadius.all(Radius.circular(6))
        ),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(activeList,index),
            _rightContent(activeList,index),
          ],
        ),
      ),
    );
  }

  Widget _leftOrderNumber(activeList,index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 50,
      child: Column(
        children: <Widget>[
          Text('序号${index}',
            textAlign: TextAlign.center,

          ),
        ],
      ),
    );
  }

  Widget _rightContent(activeList,index) {
    int postType = activeList[index].postType;
    String postTypeText = '';
    switch (postType) {
      case 0: { postTypeText = '日常动态'; }
      break;
      case 2: { postTypeText = '注册审核视频'; }
      break;
      case 3: { postTypeText = '认证视频'; }
      break;
    }

    return Container(
      padding: EdgeInsets.only(left: 8,right: 6,top: 6,bottom: 6),
      decoration: BoxDecoration(
          border: Border(
            left: BorderSide(color: Colors.grey,width: 1),
          )
      ),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户昵称:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${activeList[index].userInfo.nickName}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '动态类型:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${postTypeText}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '动态内容:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${activeList[index].postContent}'),
              ),
            ],
          ),

        ],
      ),
    );
  }
}

